#include "GamePlayState.h"
#include "GameArena.h"
#include "MainMenuState.h"
#include "GameOverState.h"
#include "MovePieceNetworkEvent.h"
#include "SoundWrapper.h"

GamePlayState::GamePlayState(ClientPtr client, unsigned int widthOfWell)
        : client(client), widthOfWell(widthOfWell)
{
}

void GamePlayState::init()
{
    SoundWrapper::playMusic("resources/tetris.mp3");
}

GraphicalElement* GamePlayState::createGraphics()
{
    return new GameArena(widthOfWell);
}

GameState* GamePlayState::handleLeftKey()
{
    MovePieceNetworkEvent event(MovePieceNetworkEvent::LEFT);
    client->send(event);
    return this;
}

GameState* GamePlayState::handleRightKey()
{
    MovePieceNetworkEvent event(MovePieceNetworkEvent::RIGHT);
    client->send(event);
    return this;
}

GameState* GamePlayState::handleDownKey()
{
    MovePieceNetworkEvent event(MovePieceNetworkEvent::DOWN);
    client->send(event);
    return this;
}

GameState* GamePlayState::handleSpaceKey()
{
    MovePieceNetworkEvent event(MovePieceNetworkEvent::INSTANT_DOWN);
    client->send(event);
    SoundWrapper::playSound("click");
    return this;
}

GameState* GamePlayState::handleUpKey()
{
    MovePieceNetworkEvent event(MovePieceNetworkEvent::ROTATE);
    client->send(event);
    return this;
}

GameState* GamePlayState::leaveGame()
{
    SoundWrapper::stopMusic();
    client->quit();
    return new MainMenuState();
}

GameState* GamePlayState::goToGameOverState(const std::string& winner)
{
    SoundWrapper::stopMusic();
    client->quit();
    return new GameOverState(winner);
}

GameState* GamePlayState::handleEvent(const Event& event)
{
    return event.visitGamePlayState(this);
}

GamePlayState::~GamePlayState()
{
}
